//
//  CollectionViewCell.m
//  DEMO3dTouh
//
//  Created by J on 16/6/16.
//  Copyright © 2016年 J. All rights reserved.
//

#import "CollectionViewCell.h"
#import <objc/runtime.h>

@implementation CollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    
    [self.contentView addGestureRecognizer:tap];
    
    
    
    for (UIGestureRecognizer*gesture in self.contentView.gestureRecognizers) {
        if ([gesture isKindOfClass:[UITapGestureRecognizer class]]){
            
            Ivar targetsIvar = class_getInstanceVariable([UIGestureRecognizer class], "_targets");
            id targetActionPairs = object_getIvar(gesture, targetsIvar);
            
            Class targetActionPairClass = NSClassFromString(@"UIGestureRecognizerTarget");
            Ivar targetIvar = class_getInstanceVariable(targetActionPairClass, "_target");
            Ivar actionIvar = class_getInstanceVariable(targetActionPairClass, "_action");
            
            for (id targetActionPair in targetActionPairs)
            {
                id target = object_getIvar(targetActionPair, targetIvar);
                SEL action = (__bridge void *)object_getIvar(targetActionPair, actionIvar);
                
                //            NSLog(@"target=%@; action=%@", target, NSStringFromSelector(action));
                
                //            [self performSelector: NSSelectorFromString(NSStringFromSelector(action)) withObject:gesture];
                self.action = NSSelectorFromString(NSStringFromSelector(action));
            }
            
        }
        
        
    }
 
}
- (void)tapGesture:(UITapGestureRecognizer *)sender {
    
//    for (UIView* next = [self superview]; next; next = next.superview) {
//        UIResponder* nextResponder = [next nextResponder];
//        if ([nextResponder isKindOfClass:[UIViewController class]]) {
//            
//            NSLog(@"%@",nextResponder);
//            
//        }
//    }

    for (UIGestureRecognizer*gesture in self.contentView.gestureRecognizers) {
        if ([gesture isKindOfClass:[UITapGestureRecognizer class]]){

        Ivar targetsIvar = class_getInstanceVariable([UIGestureRecognizer class], "_targets");
        id targetActionPairs = object_getIvar(gesture, targetsIvar);
        
        Class targetActionPairClass = NSClassFromString(@"UIGestureRecognizerTarget");
        Ivar targetIvar = class_getInstanceVariable(targetActionPairClass, "_target");
        Ivar actionIvar = class_getInstanceVariable(targetActionPairClass, "_action");
        
        for (id targetActionPair in targetActionPairs)
        {
            id target = object_getIvar(targetActionPair, targetIvar);
            SEL action = (__bridge void *)object_getIvar(targetActionPair, actionIvar);
            
            NSLog(@"target=%@; action=%@", target, NSStringFromSelector(action));
            
//            [self performSelector: NSSelectorFromString(NSStringFromSelector(action)) withObject:gesture];
            
//            self.action = NSSelectorFromString(NSStringFromSelector(action));
            
//            // 获取方法的参数列表
//            IMP method = [self methodForSelector:self.action];
//
//            int arguments = method_getNumberOfArguments(method);
//            NSLog(@"参数个数：%d", arguments);
        }

        }
    
    
    }

//    NSLog(@"%@",self);
}

@end
